package com.xb.myspring.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;

/**
 * 4InstantiationAwareBeanPostProcessor继承了BeanPostProcess接口
 * BeanPostProcess接口只在bean的初始化阶段进行扩展（注入spring上下文前后），
 * 而InstantiationAwareBeanPostProcessor接口在此基础上增加了3个方法，把可扩展的范围增加了实例化阶段和属性注入阶段。
 * 这个扩展点非常有用，无论是写中间件和业务中，都能利用这个特性。比如对实现了某一类接口的bean在各个生命期间进行收集，或者对某个类型的bean进行统一的设值等等。
 */
public class TestInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

    // 实例化bean之前，相当于new这个bean之前
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("SpringExtend-4InstantiationAwareBeanPostProcessor before initialization " + beanName);      
        return bean;      
    }      

    // 实例化bean之后，相当于new这个bean之后
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {      
        System.out.println("SpringExtend-4InstantiationAwareBeanPostProcessor after initialization " + beanName);      
        return bean;      
    }

    // bean已经实例化完成，在属性注入时阶段触发，@Autowired,@Resource等注解原理基于此方法实现
    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        System.out.println("SpringExtend-4InstantiationAwareBeanPostProcessor postProcessProperties " + beanName);
        return pvs;
    }

    // 初始化bean之前，相当于把bean注入spring上下文之前
    @Override      
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {      
        System.out.println("SpringExtend-4InstantiationAwareBeanPostProcessor before instantiation " + beanName);      
        return null;      
    }

    // 初始化bean之后，相当于把bean注入spring上下文之后
    @Override      
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {      
        System.out.println("SpringExtend-4InstantiationAwareBeanPostProcessor after instantiation " + beanName);      
        return true;      
    }

}